package com.example.ad_project;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

import com.nus.iss.ad.dbhelper.OrderDBHelper;
import com.nus.iss.ad.model.Order;
import com.nus.iss.ad.model.OrderDetail;


public class ConfirmDeliveryDetail extends BaseActivity implements 
OnClickListener,
OnItemClickListener,
OnCheckedChangeListener
{
	Button buttonClosePO;
	String orderId;
	TextView textViewOrderId;
	
	OrderDBHelper orderData=new OrderDBHelper();
	ListView lv;
	
	OrderDetail orderDetails[];
	private void getData(String orderId) {
		Order order=orderData.getOrderById(orderId);
		orderDetails=order.getPurchaseOrderDetails();
		
	}

	//@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
        Bundle extras = getIntent().getExtras();
        if (extras == null)
          return;
        orderId = extras.getString("orderId");
	    
	    StrictMode.setThreadPolicy(ThreadPolicy.LAX);
	    
	    setContentView(R.layout.activity_confirm_delivery_detail); 
	    getData(orderId);
	    lv= (ListView) findViewById(R.id.listView1);
	    lv.setAdapter(new OrderDetailAdapter(this, orderDetails));
	    
	    lv.setOnItemClickListener(this);
	    
	    buttonClosePO=(Button)findViewById(R.id.buttonClosePO);
	    buttonClosePO.setEnabled(isAllItemChecked());
	    buttonClosePO.setOnClickListener(this);
	    
	    textViewOrderId=(TextView)findViewById(R.id.textViewOrderId);
	    textViewOrderId.setText(orderId);
	}
	
	public void onItemClick(AdapterView<?> av, View v, int position, long id) {
	      OrderDetail od = (OrderDetail) av.getAdapter().getItem(position);
	      
	}

	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		//Toast.makeText(getApplicationContext(),Boolean.toString(arg1),Toast.LENGTH_SHORT).show();
		 buttonClosePO.setEnabled(isAllItemChecked());
	}

	@Override
	public void onClick(View v) {
		//Toast.makeText(getApplicationContext(),"Close PO Clicked",Toast.LENGTH_SHORT).show();
		
		//Toast.makeText(getApplicationContext(),Boolean.toString(isAllItemChecked()),Toast.LENGTH_SHORT).show();
		
		/*
		try {

			Intent intent = new Intent("com.google.zxing.client.android.SCAN");
			intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for
															// bar codes

			startActivityForResult(intent, 0);

		} catch (Exception e) {

			Uri marketUri = Uri
					.parse("market://details?id=com.google.zxing.client.android");
			Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
			startActivity(marketIntent);

		}
		*/
        
		orderData.closePO(orderId);
    	showDialog("Close Purchase Order successful",true);
		
		
	}
	
	
	/*@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {           
	    super.onActivityResult(requestCode, resultCode, data);
	    if (requestCode == 0) {

	        if (resultCode == RESULT_OK) {
	            
	        	String contents = data.getStringExtra("SCAN_RESULT");
	            Log.d("QR test", contents);
	            if(orderId.equalsIgnoreCase(contents)){
	            	
	            	orderData.closePO(orderId);
	            	showDialog("Close Purchase Order successful",true);
	        		//setResult(RESULT_OK);
	                //finish();
	            }else {
	            	Toast.makeText(getApplicationContext(),"Please Scan QR code in Purchase Order to complete ",Toast.LENGTH_LONG).show();
				}
	            
	        }
	        if(resultCode == RESULT_CANCELED){
	            //handle cancel
	        	Toast.makeText(getApplicationContext(),"Please Scan QR code in Purchase Order to complete ",Toast.LENGTH_LONG).show();
	        }
	    }
	}*/
	
	
	
	
	
	@SuppressLint("NewApi")
	private boolean isAllItemChecked(){
		ArrayList<View> outViews=new ArrayList<View>();
		lv.findViewsWithText(outViews,"Confirm",ListView.FIND_VIEWS_WITH_TEXT);
		
		boolean allItemCheck=true;
		if(outViews.size()==0){ allItemCheck=false;}
		for(View chk:outViews){
			CheckBox checkBox=(CheckBox)chk;
			if(!checkBox.isChecked()){
				allItemCheck=false;
				break;
			}
		}
		return allItemCheck;
	}
	

}
